<!-- 数据库连接管理 -->
<?php
require ( 'config.php' );

/**
 * 数据管理类
 * @author Kai
 *
 */
class DataManage
{
	//数据库连接信息
	private $address = '';
	private $port = '';
	private $username = '';
	private $password = '';
	private $database_name = ''; 
	private $charset = 'utf8';
	//数据库连接对象
	private $pdo;
	//单例对象
	private static $__instance = null;
	//上一条执行的sql命令
	private $queryString = '';
	
	/**
	 * 数据管理类构造函数
	 * @param string $database
	 */
	private function __construct($database = DB_SET)
	{
		try
		{
			global $DBManage;
			$this->address = $DBManage[$database]['address'];
			$this->port = $DBManage[$database]['port'];
			$this->username = $DBManage[$database]['username'];
			$this->password = $DBManage[$database]['password'];
			$this->database_name = $DBManage[$database]['database_name'];
			
			$this->pdo=null;
			
			try{
				//建立连接对象
				$this->pdo = new PDO(
					'mysql:host='.$this->address.
					';port='.$this->port.
					';dbname='.$this->database_name,$this->username,$this->password);
			}
			catch(PDOException $e)
			{
				//再次建立连接对象
				$this->pdo = new PDO(
						'mysql:host='.$this->address.
						';port='.$this->port.
						';dbname='.$this->database_name,$this->username,$this->password);
			}
			
			$this->pdo->exec(
					'SET NAMES \''.$this->charset.'\'');
			
			
		} 
		catch (PDOException $e) 
		{
			echo $e->getMessage();
		}
	}
	
	/**
     * 禁止克隆
	 */
	private function __clone(){;}
	
	/**
	 * 得到对象
	 */
	public static function GetInstance()
	{
		if(!self::$__instance instanceof self)
		{
			self::$__instance = new self();
		}
		return self::$__instance;
	}
	
	
	/**
	 * 查询结果数量
	 * @param unknown $table
	 * @param unknown $where
	 * @return number
	 */
	public function count($table,$where = 1)
	{
		return 0 + (
				$this->query(
						'SELECT COUNT(*) FROM '.$table.
						' WHERE '.$where)->fetch()[0]
				);
	}
	
	/**
	 * 执行有记录集返回的数据操作
	 * @param unknown $query
	 * @return PDOStatement
	 */
	public function query($query)
	{
		$this->queryString = $query;
		return $this->pdo->query($query);
	}
	
	/**
	 * 执行无记录集返回的数据操作
	 * @param unknown $query
	 * @return number
	 */
	public function exec($query)
	{
		$this->queryString = $query;
		return $this->pdo->exec($query);
	}
	
	/**
	 * 返回数据库操作错误信息
	 * @return multitype:
	 */
	public function error()
	{
		return $this->pdo->errorInfo();
	}
	
	/**
	 * 得到上一条执行过的sql语句
	 * @return string
	 */
	public function last_query()
	{
		return $this->queryString;
	}
}

/**
 * 小工具类
 * @author Kai
 *
 */
class Tool
{
	/**
	 * 获取当前服务器时间(微秒)
	 * @return mixed
	 */
	public static function getTime()
	{
		return microtime(true);
	}
	
	/**
	 * 获取当前服务器日期
	 */
	public static function getDate()
	{
		date_default_timezone_set('UTC');
		return "".date("Y-m-d");
	}
	
	/**
	 * 获取指定日期
	 * @param unknown $y
	 * @param unknown $m
	 * @param unknown $d
	 * @return string
	 */
	public static function getDateWithTime($y, $m, $d)
	{
		date_default_timezone_set('UTC');
		return "".date("Y-m-d",mktime(0, 0, 0, $m, $d, $y));
	}
}

